﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PASNew.U.Controls
{
    public partial class Login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void LBLogin_Click(object sender, EventArgs e)
        {
            lblUnsuccessfull.Visible = false;

            string useremail = txtBoxEmail.Text.ToString();
            string password = txtBoxPassword.Text.ToString();

            using (PASDataDataContext data = new PASDataDataContext())
            {
                var usercheck = (from u in data.UserDetails
                                 where u.UserEmail == useremail
                                 && u.UserPassword == password
                                 select u).FirstOrDefault();

                if (usercheck != null)
                {
                    var compident = (from cid in data.CompanyUserRelations
                                     where cid.UserId == usercheck.UserId
                                     select cid).FirstOrDefault();

                    if (usercheck != null && compident != null)
                    {
                        UserDetailsDto user = new UserDetailsDto
                                   {
                                       UserID = usercheck.UserId,
                                   };

                        Session["UserIdentity"] = user;

                        CompIDdto compIdentity = new CompIDdto
                        {
                            CompID = compident.CompanyId,
                        };

                        Session["CompanyIdentitifier"] = compIdentity;

                        if (usercheck.UserLevel == 1)
                        {
                            Response.Redirect("Backend.aspx");
                        }
                        else
                        {
                            Response.Redirect("Notifications.aspx");
                        }
                    }
                }
                else
                {
                    lblUnsuccessfull.Visible = true;
                }
            }
        }
    }
}